package com.portal.logic;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

public class CustomerSaleDAO {

	//private String ip;
	private Connection connection;
	
	public CustomerSaleDAO(Connection connection) {
		//this.ip = ip;
		this.connection = connection;
	}
	
	
	//Create
	public void insert() {
		CustomerSale customerSale = null;
		try {
			Object deserializedObject = SerializationUtil.deserialize("c:\\customer_sale.out");
			customerSale = (CustomerSale) deserializedObject;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		PreparedStatement preparedStatement = null;

		try {
			Statement st = connection.createStatement(); 
            st.executeUpdate("INSERT INTO tblCustomersSales " + 
                "VALUES ("+customerSale.getId()+","+customerSale.getCustomerId()+","+customerSale.getSaleId()+" )"); 

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			//ConnectionManager.closeConnection(connection);
		}
	}

	public CustomerSale selectById(int id) {
		CustomerSale customerSale = new CustomerSale();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement("SELECT * FROM tblCustomersSales WHERE id = ?");
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {
				customerSale.setId(resultSet.getInt("id"));
				customerSale.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
				customerSale.setSaleId(resultSet.getInt("sale_id"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}

		return customerSale;
	}

	
	//Read All
	public List<CustomerSale> selectAllCustomersPerSale(int saleId) {
		Statement statement = null;
		ResultSet resultSet = null;
		List<CustomerSale> customersSales = new LinkedList<CustomerSale>();
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM tblCustomersSales where sale_id ="+saleId+ "");

			CustomerSale customerSale = null;
			while(resultSet.next()){
				customerSale = new CustomerSale();
				
				customerSale.setId(resultSet.getInt("id"));
				customerSale.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
				customerSale.setSaleId(resultSet.getInt("sale_id"));
				
				customersSales.add(customerSale);
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		System.out.println(customersSales);
		return customersSales;
	}

	//Read All
	public List<CustomerSale> selectAll() {
		Statement statement = null;
		ResultSet resultSet = null;
		List<CustomerSale> customersSales = new LinkedList<CustomerSale>();
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT * FROM tblCustomersSales");

			CustomerSale customerSale = null;
			while(resultSet.next()){
				customerSale = new CustomerSale();
				
				customerSale.setId(resultSet.getInt("id"));
				customerSale.setCustomerId(Integer.parseInt(resultSet.getString("customer_id")));
				customerSale.setSaleId(resultSet.getInt("sale_id"));
				
				customersSales.add(customerSale);
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		System.out.println(customersSales);
		return customersSales;
	}	
	

	//Update
	public void update() {
		CustomerSale customerSale = null;
		try {
			Object deserializedObject = SerializationUtil.deserialize("c:\\editCustomer_sale.out");
			customerSale = (CustomerSale) deserializedObject;
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		PreparedStatement preparedStatement = null;

		try {
			preparedStatement = connection.prepareStatement("UPDATE tblCustomersSales SET " +
					"customer_id = ?, sale_id = ? WHERE id = ?");

			preparedStatement.setInt(1, customerSale.getCustomerId());
			preparedStatement.setInt(2, customerSale.getSaleId());
			preparedStatement.setInt(3, customerSale.getId());
			preparedStatement.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
	}

	//Delete
	public void delete(int id) {
		PreparedStatement preparedStatement = null;

		try {
			preparedStatement = connection.prepareStatement("DELETE FROM tblCustomersSales WHERE id = ?");
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();

			System.out.println("DELETE FROM tblCustomersSales WHERE id = ?");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
	}

	
	public int selectMaxId() {
		Statement statement = null;
		ResultSet resultSet = null;
		int id = 0;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery("SELECT max(id) as max_val FROM tblCustomersSales");

			while(resultSet.next()){
				id = Integer.parseInt(resultSet.getString("max_val"));
			}

			/*resultSet.close();
			statement.close();*/
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//ConnectionManager.closeConnection(connection);
		}
		
		return id;
	}
}
